const formatTime = date => {
  var weekday=new Array(7)
  weekday[0]="星期日"
  weekday[1]="星期一"
  weekday[2]="星期二"
  weekday[3]="星期三"
  weekday[4]="星期四"
  weekday[5]="星期五"
  weekday[6]="星期六"
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  const wday = weekday[date.getDay()]
  return [year, month, day].map(formatNumber).join('/') +' '+ [wday].map(formatNumber).join(' ')+' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const isVailValue = o =>{
  return o&&o!==undefined&&o!=='undefined'&&o!==null&&o!==''?true:false;
}
const get = (url,callback,isCustomLoading)=>{
  if(!isCustomLoading){
    showBusy("正在加载中...");
  }
  wx.request({
    url: url,
    method:'GET',
    header: {
      'content-type': 'application/json'
    },
    success: function(res) {
      console.log(res.data);
      wx.hideToast();
      if(callback){
        if(res.data.errorCode===0){
          callback(res.data.responseData);
        }else{
          showModel('米粒提示',res.data.errorMsg);
        }
      }
    },
    fail: function(res) {
      // console.log(res);
      // showModel('网络异常','数据出现异常，一会儿再试试吧~');
      showModel('网络异常',res);
    }
  })
}
const post = (url,param,callback,isCustomLoading)=>{
  if(!isCustomLoading){
    showBusy("正在加载中...");
  };
  wx.request({
    url: url,
    data:param,
    method:'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      // console.log(res.data);
      wx.hideToast();
      if(callback){
        if(res.data.errorCode===0){
          callback(res.data.responseData);
        }else{
          showModel('米粒提示',res.data.errorMsg);
        }
      }
    },
    fail: function(res) {
      // console.log(res);
      showModel('网络异常','数据出现异常，一会儿再试试吧~');
    }
  })
}
//获取屏幕宽高
const getSystemHW = ()=>{
  var windowSize = {};
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowscale = windowHeight/windowWidth;//屏幕高宽比
      windowSize.windowWidth = windowWidth;
      windowSize.windowHeight = windowHeight;
      windowSize.windowscale = windowscale;
    }
  });
  return windowSize;
}
// 获取dom坐标位置
const getSeletorLoction = (id,obj,callback)=>{
  var query = wx.createSelectorQuery().in(obj)
  query.select('#'+id).boundingClientRect(function(res){
    if(callback){
      callback(res);
    }
  }).exec();
}
// 图片等比例缩放
const imageUtil = (e) =>{
  var imageSize = {};
  var originalWidth = e.detail.width;//图片原始宽
  var originalHeight = e.detail.height;//图片原始高
  var originalScale = originalHeight/originalWidth;//图片高宽比
  //获取屏幕宽高
  var windowobj= getSystemHW();
  var windowWidth = windowobj.windowWidth;
  var windowHeight = windowobj.windowHeight;
  var windowscale = windowHeight/windowWidth;//屏幕高宽比
  if(originalScale < windowscale){//图片高宽比小于屏幕高宽比
    //图片缩放后的宽为屏幕宽
     imageSize.imageWidth = windowWidth;
     imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
     imageSize.windowWidth = windowWidth;
     imageSize.windowHeight = windowHeight;
  }else{
    //图片高宽比大于屏幕高宽比
    //图片缩放后的高为屏幕高
     imageSize.imageHeight = windowHeight;
     imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
     imageSize.windowWidth = windowWidth;
     imageSize.windowHeight = windowHeight;
  };
  return imageSize;
}
// 设置存储本地信息(同步)
var setStorageInfo = (data)=>{
  try {
    wx.setStorageSync(data.key, data.value)
  } catch (e) {
    console.log(data.key+"存储本地信息异常");
  }
}
// 获取存储本地信息(同步)
var getStorageInfo = (key)=>{
  try {
    var value = wx.getStorageSync(key)
    if (value) {
        return value;
    }else{
      return '-1000';
    }
  } catch (e) {
    console.log(data.key+"获取存储本地信息异常");
  }
}
// 显示繁忙提示
var showBusy = text => wx.showToast({
  title: text,
  icon: 'loading',
  duration: 10000
})

// 显示成功提示
var showSuccess = text => wx.showToast({
  title: text,
  icon: 'success'
})
// 显示存粹提示
var showTips = text => wx.showToast({
  title: text,
  image:"../../lib/images/error.png"
})
// 显示失败提示
var showModel = (title, content) => {
  wx.hideToast();
  wx.showModal({
    title,
    content: JSON.stringify(content),
    showCancel: false
  })
}
var stringTrim = (str) =>{
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
module.exports = {
  formatTime: formatTime,
  formatNumber:formatNumber,
  get:get,
  post:post,
  isVailValue:isVailValue,
  showBusy:showBusy,
  showSuccess:showSuccess,
  showModel:showModel,
  getSystemHW:getSystemHW,
  imageUtil:imageUtil,
  getSeletorLoction:getSeletorLoction,
  getStorageInfo:getStorageInfo,
  setStorageInfo:setStorageInfo,
  stringTrim:stringTrim,
  showTips:showTips
}
